#include "admin.h"
#include "ui_admin.h"

admin::admin(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::admin)
{
    ui->setupUi(this);
    //mysql_init();
}

admin::~admin()
{
    delete ui;
}

void admin::mysql_init()
{
    QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("127.0.0.1");
    db.setUserName("root"); //数据库账号
    db.setPassword("123456");//数据库密码
    //db.setPort(3306);
    db.setDatabaseName("restaurtal"); //你的数据库名
    if(!db.open())
    {
        qDebug()<<"连接数据库失败";
    }else
    {
        qDebug()<<"连接数据库成功";
    }
}

void admin::on_pushButton_clicked()
{
    emit mySignal();
}

void admin::on_pushButton_3_clicked()
{
    QString a=ui->lineEdit->text();
    QString sql=QString("delete from info where id=%1").arg(a);
    QSqlQuery query;
    if(query.exec(sql))            //执行成功返回1
    {
        QMessageBox::information(this,"人员管理","删除成功");
        statusBar()->showMessage("success"+sql);
        ui->lineEdit->clear();
        ui->lineEdit_2->clear();
    }
    else
    {
        statusBar()->showMessage(query.lastError().text());     //错误返回错误信息
    }
}

void admin::on_pushButton_2_clicked()
{
    QString a=ui->lineEdit->text();
    QString b=ui->lineEdit_2->text();
    QString sql=QString("insert into info (id,password,permit) values(%1,'%2',0)").arg(a).arg(b);
    QSqlQuery query;
    if(query.exec(sql))            //执行成功返回1
    {
        QMessageBox::information(this,"人员管理","添加成功");
        statusBar()->showMessage("success"+sql);
        ui->lineEdit->clear();
        ui->lineEdit_2->clear();
    }
    else
    {
        statusBar()->showMessage(query.lastError().text());     //错误返回错误信息
    }
}

void admin::on_pushButton_4_clicked()
{
    ui->tableWidget->clear();
    QSqlQuery query;
    if(query.exec("select* from doller"))            //执行成功返回1
    {
        //QMessageBox::information(this,"人员管理","删除成功");
        statusBar()->showMessage("success");
        //ui->lineEdit->clear();
        //ui->lineEdit_2->clear();
    }
    else
    {
        statusBar()->showMessage(query.lastError().text());     //错误返回错误信息
    }
    QStringList fname;
    fname<<"单号";
    fname<<"收款";
    ui->tableWidget->setRowCount(query.size());
    ui->tableWidget->setColumnCount(2);
    ui->tableWidget->setHorizontalHeaderLabels(fname);
    for(int i=0;i<query.size();i++)
    {
        query.next();
        for(int j=0;j<2;j++)
        {
            ui->tableWidget->setItem(i,j,new QTableWidgetItem(query.value(j).toString()));
        }
     }
}
